% function out = frameSeg11( str, nPhases)
str = 'noise0.01.bmp';
nPhases = 4;
global HD
global HR
HD = frameFilterD('linear');
HR = frameFilterR('linear');
I = readImage( str ); 
%%
nu = 10;
beta = 1;
nLevels = 1;
[c label] = Kmeans(I, nPhases, 50);
c = sort(c);
M = size(I,1);
N = size(I,2);
f = zeros(M,N,nPhases);
for i = 1 : nPhases
    f(:,:,i) = nu*(abs(I - c(i))).^beta;
end
u = zeros(M,N,nPhases);
for i = 1 : nPhases;
    y{i} = frameDec2(I,nLevels,HD);
%     ubar(:,:,i) = y{1}{2}(1:M,1:N);
end
ubar = u;
%%
nIterations = 20;
delta =0.81;
tau = 0.5;
for iter = 1 : nIterations
    for i = 1 : nPhases
            tmp = frameDec2(ubar(:,:,i), nLevels, HD);
            y{i} = cellPlus(y{i}, cellMultiply(tmp,delta));
            y{i} = thresholdCell(y{i},1);
            u(:,:,i) = u(:,:,i) - tau.*frameRec2(y{i},nLevels,0,HR) - tau.*f(:,:,i) ;
    end
     u= shiftdim(projSplx(shiftdim(u,2)),1);  
     ubar = 2*u - ubar;
     Energy_W11(u,f,nPhases,nLevels)
end
[Y U] = max(u,[],3);
output = c(U);
imshow(output,[]);
% end




    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    